﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Retete.Models;
using Retete.ViewModels;
using System.Text;
using System.IO;
using System.Globalization;
using System.Configuration;
using System.Diagnostics;
using Retete.Helpers;

namespace Retete.Controllers
{
    public class VanzariController : Controller
    {
        private ReteteEntities entities = new ReteteEntities();

        public ActionResult Index(int? year, int? month, int? day)
        {
            if (day.HasValue == true)
            {
                DateTime selectedDate = new DateTime(year.Value, month.Value, day.Value);
                List<Vanzare> selectieVanzari = entities.Vanzari.Where(element => element.Data.Value == selectedDate).ToList();

                return View(new VanzariViewModel() { Vanzari = selectieVanzari, SelectedDate = selectedDate });
            }
            else return View(new VanzariViewModel() { Vanzari = entities.Vanzari.Where(element => element.Data.Value == DateTime.Today).ToList(), SelectedDate = DateTime.Today });
        }
        public ActionResult Create()
        {
            List<string> tipuriVanzare = new List<string>() { "Plata numerar", "Plata prin virament", "Masa personal", "Marfa degradata" };
            var bonuriExistente = from element in entities.VanzariBonuri
                                  select new VanzareBonViewModel() { ValoareBon = element.ValoareBon, Numar = 0 };

            return View(new VanzareViewModel(bonuriExistente.Distinct().ToList()) { TipuriVanzare = tipuriVanzare, Data = DateTime.Today, EmiteBon = true, TiparesteProdusele = true });
        }
        public ActionResult CentralizatorVanzari()
        {
            var vanzari = from elementVanzare in entities.ElementeVanzari
                          join produs in entities.Produse on elementVanzare.IDProdus equals produs.ID into vanzariProduse
                          from produs in vanzariProduse.DefaultIfEmpty()
                          join reteta in entities.Retete on elementVanzare.IDReteta equals reteta.ID into vanzariRetete
                          from reteta in vanzariRetete.DefaultIfEmpty()
                          join meniu in entities.Meniuri on elementVanzare.IDMeniu equals meniu.ID into vanzariMeniuri
                          from meniu in vanzariMeniuri.DefaultIfEmpty()
                          where elementVanzare.Vanzare.Data.Value == DateTime.Today
                          select new
                          {
                              IDVanzare = elementVanzare.IDProdus ?? elementVanzare.IDReteta ?? elementVanzare.IDMeniu,
                              NumeVanzare = produs.Nume ?? reteta.Nume ?? meniu.Denumire,
                              Cod = produs.Cod ?? reteta.Cod ?? meniu.Cod,
                              TipVanzare =
                              elementVanzare.IDProdus.HasValue == true ? "P" :
                              elementVanzare.IDReteta.HasValue == true ? "R" :
                              elementVanzare.IDMeniu.HasValue == true ? "M" : null,
                              Cantitate = elementVanzare.Portii,
                              Pret = elementVanzare.Pret,
                              CategorieVanzare = elementVanzare.Vanzare.Tip
                          };
            var grupeVanzari = from elementVanzare in vanzari
                               group elementVanzare by new { CategorieVanzare = elementVanzare.CategorieVanzare, TipVanzare = elementVanzare.TipVanzare, NumeVanzare = elementVanzare.NumeVanzare, Cod = elementVanzare.Cod, IDVanzare = elementVanzare.IDVanzare } into grup
                               select new CentralizatorElementViewModel() { TipVanzare = grup.Key.CategorieVanzare, Produs = grup.Key.NumeVanzare, Cod = grup.Key.Cod, Portii = grup.Sum(element => element.Cantitate), Pret = grup.Average(element => element.Pret) };

            var categorii = from element in entities.Produse
                            orderby element.Categorie
                            select element.Categorie;

            return View(new CentralizatorViewModel(grupeVanzari.OrderBy(element => element.Produs).ToList(), categorii.Distinct().ToList(), DateTime.Today));
        }
        public ActionResult BonConsum()
        {
            List<StocViewModel> vanzari = Rapoarte.GenereazaVanzari(DateTime.Today, string.Empty);
            List<string> categorii = (from element in vanzari
                                      select element.Categorie).Distinct().ToList();

            categorii.Insert(0, string.Empty);

            return View(new BonConsumViewModel() { Data = DateTime.Today, Categorie = string.Empty, Elemente = vanzari, Categorii = categorii });
        }
        public ActionResult RaportProductie()
        {
            List<StocViewModel> vanzari = Rapoarte.GenereazaVanzariSimple(DateTime.Today);
            List<string> categorii = (from element in vanzari
                                      select element.Categorie).Distinct().ToList();

            categorii.Insert(0, string.Empty);

            return View(new BonConsumViewModel() { Data = DateTime.Today, Categorie = string.Empty, Elemente = vanzari, Categorii = categorii });
        }
        public ActionResult BalantaStocuri()
        {
            List<StocViewModel> stocIntrare = Rapoarte.GenereazaStoc(new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1));
            List<StocViewModel> stocIesire = Rapoarte.GenereazaStoc(new DateTime(DateTime.Today.Year, DateTime.Today.Month, new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(1).AddDays(-1).Day));
            List<StocViewModel> vanzari = Rapoarte.GenereazaVanzariLunare(DateTime.Today.Month, DateTime.Today.Year, string.Empty);
            List<StocViewModel> receptii = Rapoarte.GenereazaReceptiiLunare(DateTime.Today.Month, DateTime.Today.Year);

            var rezultat = from elementIntrare in stocIntrare
                           join elementIesire in stocIesire on elementIntrare.ID equals elementIesire.ID
                           join elementVanzare in vanzari on elementIntrare.ID equals elementVanzare.ID into vanzariJoin
                           from elementVanzare in vanzariJoin.DefaultIfEmpty()
                           join elementReceptie in receptii on elementIntrare.ID equals elementReceptie.ID into receptiiJoin
                           from elementReceptie in receptiiJoin.DefaultIfEmpty()
                           select new RaportProductieElementViewModel()
                           {
                               Cod = elementIntrare.Cod,
                               Produs = elementIntrare.Nume,
                               Categorie = elementIntrare.Categorie,
                               StocInitial = elementIntrare.Cantitate,
                               StocFinal = elementIesire.Cantitate,
                               CantitateVanduta = elementVanzare == null ? 0 : elementVanzare.Cantitate,
                               CantitateCumparata = elementReceptie == null ? 0 : elementReceptie.Cantitate,
                               PretCumparare = elementIntrare.PretCumparare,
                               PretVanzare = elementIntrare.PretCumparare
                           };

            return View(new RaportProductieViewModel() { Elemente = rezultat.Where(element => element.CantitateCumparata > 0 || element.CantitateVanduta > 0).OrderBy(element => element.Produs).ToList(), Data = DateTime.Today });
        }
        public ActionResult RaportGestiuneBauturi()
        {
            List<StocViewModel> stocIntrare = Rapoarte.GenereazaStoc(DateTime.Today.AddDays(-1));
            List<StocViewModel> stocIesire = Rapoarte.GenereazaStoc(DateTime.Today);
            List<StocViewModel> vanzari = Rapoarte.GenereazaVanzari(DateTime.Today, string.Empty);
            List<StocViewModel> receptii = Rapoarte.GenereazaReceptii(DateTime.Today);

            var rezultat = from elementIntrare in stocIntrare
                           join elementIesire in stocIesire on elementIntrare.ID equals elementIesire.ID
                           join elementVanzare in vanzari on elementIntrare.ID equals elementVanzare.ID into vanzariJoin
                           from elementVanzare in vanzariJoin.DefaultIfEmpty()
                           join elementReceptie in receptii on elementIntrare.ID equals elementReceptie.ID into receptiiJoin
                           from elementReceptie in receptiiJoin.DefaultIfEmpty()
                           select new RaportProductieElementViewModel()
                           {
                               Cod = elementIntrare.Cod,
                               Produs = elementIntrare.Nume,
                               Categorie = elementIntrare.Categorie,
                               StocInitial = elementIntrare.Cantitate,
                               StocFinal = elementIesire.Cantitate,
                               CantitateVanduta = elementVanzare == null ? 0 : elementVanzare.Cantitate,
                               CantitateCumparata = elementReceptie == null ? 0 : elementReceptie.Cantitate,
                               PretCumparare = elementIntrare.PretCumparare,
                               PretVanzare = elementIntrare.Pret
                           };
            var bauturi = rezultat.Where(element => element.Categorie == "Bauturi");

            return View(new RaportProductieViewModel() { Elemente = bauturi.Where(element => element.CantitateCumparata > 0 || element.CantitateVanduta > 0).OrderBy(element => element.Produs).ToList(), Data = DateTime.Today });
        }
        public ActionResult Details(int id)
        {
            return View(entities.Vanzari.FirstOrDefault(element => element.ID == id));
        }

        [HttpPost]
        public ActionResult Index(VanzariViewModel result)
        {
            if (result.IDElementSters != 0)
            {
                foreach (VanzareBon bon in entities.VanzariBonuri.Where(element => element.IDVanzare == result.IDElementSters)) entities.DeleteObject(bon);
                foreach (ElementVanzare elementVanzare in entities.ElementeVanzari.Where(element => element.IDVanzare == result.IDElementSters)) entities.DeleteObject(elementVanzare);
                entities.DeleteObject(entities.Vanzari.First(element => element.ID == result.IDElementSters));
                entities.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Create(VanzareViewModel result)
        {
            if (result.Refresh == true)
            {
                result.Refresh = false;
                ModelState.Clear();

                return View(result);
            }
            else if (result.NumarBonuriNoi > 0)
            {
                if (result.Bonuri == null) result.Bonuri = new List<VanzareBonViewModel>();
                result.Bonuri.Add(new VanzareBonViewModel() { ValoareBon = result.ValoareBonNou, Numar = result.NumarBonuriNoi });

                result.NumarBonuriNoi = 0;
                result.ValoareBonNou = 0;
                ModelState.Clear();
                return View(result);
            }
            else if (result.IDElementSters != -1)
            {
                ElementVanzareViewModel produsSters = result.ElementeVanzare.First(element => element.ID == result.IDElementSters);
                result.ElementeVanzare.Remove(produsSters);
                result.IDElementSters = -1;

                ModelState.Clear();
                return View(result);
            }
            else if (result.IDMeniuVandut != -1 || result.IDRetetaVanduta != -1 || result.IDProdusVandut != -1)
            {
                if (result.ElementeVanzare == null) result.ElementeVanzare = new List<ElementVanzareViewModel>();

                ElementVanzareViewModel nou = new ElementVanzareViewModel();
                if (result.IDMeniuVandut != -1)
                {
                    Meniu meniuSelectat = entities.Meniuri.First(element => element.ID == result.IDMeniuVandut);

                    nou.IDMeniu = meniuSelectat.ID;
                    nou.Nume = meniuSelectat.Denumire;
                    nou.Cod = meniuSelectat.Cod;
                    nou.Pret = result.PretVanzare;
                    nou.Portii = result.PortiiVandute;
                }
                else if (result.IDRetetaVanduta != -1)
                {
                    Reteta retetaSelectata = entities.Retete.First(element => element.ID == result.IDRetetaVanduta);

                    nou.IDReteta = retetaSelectata.ID;
                    nou.Nume = retetaSelectata.Nume;
                    nou.Cod = retetaSelectata.Cod;
                    nou.Pret = result.PretVanzare;
                    nou.Portii = result.PortiiVandute;
                }
                else if (result.IDProdusVandut != -1)
                {
                    Produs produsSelectat = entities.Produse.First(element => element.ID == result.IDProdusVandut);

                    nou.IDProdus = produsSelectat.ID;
                    nou.Nume = produsSelectat.Nume;
                    nou.Cod = produsSelectat.Cod;
                    nou.Pret = result.PretVanzare;
                    nou.Portii = result.PortiiVandute;
                }

                if (result.ElementeVanzare.Count == 0) nou.ID = 1;
                else nou.ID = result.ElementeVanzare.Max(element => element.ID) + 1;

                result.ElementeVanzare.Add(nou);
                result.IDRetetaVanduta = -1;
                result.IDMeniuVandut = -1;
                result.IDProdusVandut = -1;
                result.PortiiVandute = 0;
                result.PretVanzare = 0;
                result.DenumireVanzare = string.Empty;

                ModelState.Clear();
                return View(result);
            }
            else if (result.InregistreazaVanzare == true)
            {
                if (result.ElementeVanzare != null && result.ElementeVanzare.Count > 0)
                {
                    Vanzare vanzare = new Vanzare() { Data = result.Data, Tip = result.TipVanzare, ClientNume = result.ClientNume, ClientAdresa = result.ClientAdresa, ClientBanca = result.ClientBanca, ClientCodFiscal = result.ClientCodFiscal, ClientCodRegistruComert = result.ClientCodRegistruComert, ClientCont = result.ClientCont, ClientJudet = result.ClientJudet, FacturaNumar = result.FacturaNumar, FacturaSerie = result.FacturaSerie };
                    foreach (ElementVanzareViewModel element in result.ElementeVanzare)
                    {
                        if (element.IDReteta != -1) vanzare.ElementeVanzare.Add(new ElementVanzare() { IDReteta = element.IDReteta, Portii = element.Portii, Pret = element.Pret });
                        else if (element.IDMeniu != -1) vanzare.ElementeVanzare.Add(new ElementVanzare() { IDMeniu = element.IDMeniu, Portii = element.Portii, Pret = element.Pret });
                        else if (element.IDProdus != -1) vanzare.ElementeVanzare.Add(new ElementVanzare() { IDProdus = element.IDProdus, Portii = element.Portii, Pret = element.Pret });
                    }
                    if (result.Bonuri != null) foreach (VanzareBonViewModel element in result.Bonuri) vanzare.Bonuri.Add(new VanzareBon() { NumarBonuri = element.Numar, ValoareBon = element.ValoareBon });

                    entities.AddToVanzari(vanzare);
                    entities.SaveChanges();

                    // obtin elementele din vanzare gupate dupa ID
                    List<ElementVanzareViewModel> elementeVanzareGrupate = (from element in result.ElementeVanzare
                                                                            group element by new { IDMeniu = element.IDMeniu, IDReteta = element.IDReteta, IDProdus = element.IDProdus, Pret = element.Pret, Nume = element.Nume, Cod = element.Cod } into grupe
                                                                            select new ElementVanzareViewModel { IDMeniu = grupe.Key.IDMeniu, IDReteta = grupe.Key.IDReteta, IDProdus = grupe.Key.IDProdus, Pret = grupe.Key.Pret, Nume = grupe.Key.Nume, Cod = grupe.Key.Cod, Portii = grupe.Sum(suma => suma.Portii) }).ToList();

                    // tipareste bon de casa daca trebuie
                    if (result.EmiteBon == true)
                    {
                        using (StreamWriter writer = new StreamWriter(ConfigurationManager.AppSettings["FPrintInput"]))
                        {
                            foreach (ElementVanzareViewModel element in elementeVanzareGrupate) writer.WriteLine("S,1,______,_,__;" + (element.Nume.Length > 19 ? element.Nume.Substring(0, 19) : element.Nume) + ";" + element.Pret.ToString("0.00", CultureInfo.InvariantCulture) + ";" + element.Portii + ";1;1;1;0;0;");

                            if (result.Bonuri != null && result.Bonuri.Sum(bon => bon.Valoare) > 0)
                            {
                                writer.WriteLine("T,1,______,_,__;3;" + result.Bonuri.Sum(bon => bon.Valoare).ToString("0.00", CultureInfo.InvariantCulture) + ";;;;");
                                if (result.Bonuri.Sum(bon => bon.Valoare) < result.ElementeVanzare.Sum(element => element.Valoare)) writer.WriteLine("T,1,______,_,__;0;;;;;");
                            }
                            else if (result.TipVanzare == "Plata prin virament") writer.WriteLine("T,1,______,_,__;2;" + result.ElementeVanzare.Sum(element => element.Valoare).ToString("0.00", CultureInfo.InvariantCulture) + ";;;;");
                            else writer.WriteLine("T,1,______,_,__;0;;;;;"); // plata numerar
                        }
                        Process.Start(new ProcessStartInfo() { WorkingDirectory = ConfigurationManager.AppSettings["FPrintPath"], FileName = ConfigurationManager.AppSettings["FPrintExe"], Arguments = ConfigurationManager.AppSettings["FPrintInput"] });
                    }

                    // tipareste factura
                    if (result.TiparesteFactura == true) return Content(Rapoarte.GenereazaFactura(result, elementeVanzareGrupate));
                    else return RedirectToAction("Create");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Nu ati vandut nici un produs");
                    return View(result);
                }
            }
            else return View(result);
        }

        [HttpPost]
        public ActionResult CentralizatorVanzari(CentralizatorViewModel result)
        {
            if (result.ExportExcel == true)
            {
                result.ExportExcel = false;
                ModelState.Clear();

                return Content(Rapoarte.GenereazaRaportCentralizator(result.Data, result.Elemente));
            }
            else
            {
                var vanzari = from elementVanzare in entities.ElementeVanzari
                              join produs in entities.Produse on elementVanzare.IDProdus equals produs.ID into vanzariProduse
                              from produs in vanzariProduse.DefaultIfEmpty()
                              join reteta in entities.Retete on elementVanzare.IDReteta equals reteta.ID into vanzariRetete
                              from reteta in vanzariRetete.DefaultIfEmpty()
                              join meniu in entities.Meniuri on elementVanzare.IDMeniu equals meniu.ID into vanzariMeniuri
                              from meniu in vanzariMeniuri.DefaultIfEmpty()
                              where elementVanzare.Vanzare.Data.Value == result.Data.Date
                              select new
                              {
                                  IDVanzare = elementVanzare.IDProdus ?? elementVanzare.IDReteta ?? elementVanzare.IDMeniu,
                                  NumeVanzare = produs.Nume ?? reteta.Nume ?? meniu.Denumire,
                                  Cod = produs.Cod ?? reteta.Cod ?? meniu.Cod,
                                  TipVanzare =
                                  elementVanzare.IDProdus.HasValue == true ? "P" :
                                  elementVanzare.IDReteta.HasValue == true ? "R" :
                                  elementVanzare.IDMeniu.HasValue == true ? "M" : null,
                                  Cantitate = elementVanzare.Portii,
                                  Pret = elementVanzare.Pret, 
                                  CategorieVanzare = elementVanzare.Vanzare.Tip
                              };
                var grupeVanzari = from elementVanzare in vanzari
                                   group elementVanzare by new { CategorieVanzare = elementVanzare.CategorieVanzare, TipVanzare = elementVanzare.TipVanzare, NumeVanzare = elementVanzare.NumeVanzare, Cod = elementVanzare.Cod, IDVanzare = elementVanzare.IDVanzare } into grup
                                   select new CentralizatorElementViewModel() { TipVanzare = grup.Key.CategorieVanzare, Produs = grup.Key.NumeVanzare, Cod = grup.Key.Cod, Portii = grup.Sum(element => element.Cantitate), Pret = grup.Average(element => element.Pret) };

                var categorii = from element in entities.Produse
                                orderby element.Categorie
                                select element.Categorie;

                return View(new CentralizatorViewModel(grupeVanzari.OrderBy(element => element.Produs).ToList(), categorii.Distinct().ToList(), result.Data));
            }
        }

        [HttpPost]
        public ActionResult RaportProductie(BonConsumViewModel result)
        {
            if (result.ExportExcel == true)
            {
                result.ExportExcel = false;
                ModelState.Clear();

                return Content(Rapoarte.GenereazaRaportProductieNou(result.Data, result.Categorie, Rapoarte.GenereazaVanzari(result.Data, result.Categorie), Rapoarte.GenereazaVanzariSimple(result.Data)));
            }
            else
            {
                List<StocViewModel> vanzari = Rapoarte.GenereazaVanzariSimple(result.Data);
                List<string> categorii = (from element in Rapoarte.GenereazaVanzari(result.Data, string.Empty)
                                          select element.Categorie).Distinct().ToList();

                categorii.Insert(0, string.Empty);

                return View(new BonConsumViewModel() { Data = result.Data, Categorie = result.Categorie, Elemente = vanzari, Categorii = categorii });
            }
        }

        [HttpPost]
        public ActionResult BalantaStocuri(RaportProductieViewModel result)
        {
            if (result.ExportExcel == true)
            {
                result.ExportExcel = false;
                ModelState.Clear();

                return Content(Rapoarte.GenereazaRaportProductie(result.Data, result.Elemente));
            }
            else
            {
                List<StocViewModel> stocIntrare = Rapoarte.GenereazaStoc(result.Data.AddDays(-1));
                List<StocViewModel> stocIesire = Rapoarte.GenereazaStoc(result.Data);
                List<StocViewModel> vanzari = Rapoarte.GenereazaVanzari(result.Data, string.Empty);
                List<StocViewModel> receptii = Rapoarte.GenereazaReceptii(result.Data);

                var rezultat = from elementIntrare in stocIntrare
                               join elementIesire in stocIesire on elementIntrare.ID equals elementIesire.ID
                               join elementVanzare in vanzari on elementIntrare.ID equals elementVanzare.ID into vanzariJoin
                               from elementVanzare in vanzariJoin.DefaultIfEmpty()
                               join elementReceptie in receptii on elementIntrare.ID equals elementReceptie.ID into receptiiJoin
                               from elementReceptie in receptiiJoin.DefaultIfEmpty()
                               select new RaportProductieElementViewModel()
                               {
                                   Cod = elementIntrare.Cod,
                                   Produs = elementIntrare.Nume,
                                   Categorie = elementIntrare.Categorie,
                                   StocInitial = elementIntrare.Cantitate,
                                   StocFinal = elementIesire.Cantitate,
                                   CantitateVanduta = elementVanzare == null ? 0 : elementVanzare.Cantitate,
                                   CantitateCumparata = elementReceptie == null ? 0 : elementReceptie.Cantitate,
                                   PretCumparare = elementIntrare.PretCumparare,
                                   PretVanzare = elementIntrare.PretCumparare
                               };

                return View(new RaportProductieViewModel() { Elemente = rezultat.Where(element => element.CantitateCumparata > 0 || element.CantitateVanduta > 0).OrderBy(element => element.Produs).ToList(), Data = result.Data });
            }
        }

        [HttpPost]
        public ActionResult RaportGestiuneBauturi(RaportProductieViewModel result)
        {
            if (result.ExportExcel == true)
            {
                result.ExportExcel = false;
                ModelState.Clear();

                return Content(Rapoarte.GenereazaRaportProductie(result.Data, result.Elemente));
            }
            else
            {
                List<StocViewModel> stocIntrare = Rapoarte.GenereazaStoc(result.Data.AddDays(-1));
                List<StocViewModel> stocIesire = Rapoarte.GenereazaStoc(result.Data);
                List<StocViewModel> vanzari = Rapoarte.GenereazaVanzari(result.Data, string.Empty);
                List<StocViewModel> receptii = Rapoarte.GenereazaReceptii(result.Data);

                var rezultat = from elementIntrare in stocIntrare
                               join elementIesire in stocIesire on elementIntrare.ID equals elementIesire.ID
                               join elementVanzare in vanzari on elementIntrare.ID equals elementVanzare.ID into vanzariJoin
                               from elementVanzare in vanzariJoin.DefaultIfEmpty()
                               join elementReceptie in receptii on elementIntrare.ID equals elementReceptie.ID into receptiiJoin
                               from elementReceptie in receptiiJoin.DefaultIfEmpty()
                               select new RaportProductieElementViewModel()
                               {
                                   Cod = elementIntrare.Cod,
                                   Produs = elementIntrare.Nume,
                                   Categorie = elementIntrare.Categorie,
                                   StocInitial = elementIntrare.Cantitate,
                                   StocFinal = elementIesire.Cantitate,
                                   CantitateVanduta = elementVanzare == null ? 0 : elementVanzare.Cantitate,
                                   CantitateCumparata = elementReceptie == null ? 0 : elementReceptie.Cantitate,
                                   PretCumparare = elementIntrare.PretCumparare,
                                   PretVanzare = elementIntrare.Pret
                               };
                var bauturi = rezultat.Where(element => element.Categorie == "Bauturi");

                return View(new RaportProductieViewModel() { Elemente = bauturi.Where(element => element.CantitateCumparata > 0 || element.CantitateVanduta > 0).OrderBy(element => element.Produs).ToList(), Data = result.Data });
            }
        }

        [HttpPost]
        public ActionResult BonConsum(BonConsumViewModel result)
        {
            if (result.ExportExcel == true)
            {
                result.ExportExcel = false;
                ModelState.Clear();

                return Content(Rapoarte.GenereazaRaportBonConsum(result.Data, result.Categorie, Rapoarte.GenereazaVanzari(result.Data, result.Categorie)));
            }
            else
            {
                List<StocViewModel> vanzari = Rapoarte.GenereazaVanzari(result.Data, result.Categorie);
                List<string> categorii = (from element in Rapoarte.GenereazaVanzari(result.Data, string.Empty)
                                          select element.Categorie).Distinct().ToList();

                categorii.Insert(0, string.Empty);

                return View(new BonConsumViewModel() { Data = result.Data, Categorie = result.Categorie, Elemente = vanzari, Categorii = categorii });
            }
        }
    }
}